package com.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manage.mapper.CommentReplysMapper;
import com.manage.pojo.CommentReplys;
import com.manage.utils.PagedResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;

@Service
public class CommentReplysServiceImpl implements CommentReplysService{

    @Autowired
    private CommentReplysMapper commentReplysMapper;
    @Autowired
    private Sid rid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryCommentReplysIsExit(String commentReplyId) {
        CommentReplys commentReply = new CommentReplys();
        commentReply.setCommentReplyId(commentReplyId);
        CommentReplys result = commentReplysMapper.selectOne(commentReply);
        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void saveCommentReplys(CommentReplys commentReply) {
        String commentReplyId = rid.nextShort();
        commentReply.setCommentReplyId(commentReplyId);
        commentReplysMapper.insert(commentReply);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void commentReplyDelete(String commentReplyId) {
        Example commentReplyExample = new Example(CommentReplys.class);
        Criteria criteria = commentReplyExample.createCriteria();
        criteria.andEqualTo("commentReplyId", commentReplyId);
        commentReplysMapper.deleteByExample(commentReplyExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public CommentReplys queryCommentReplysInfoByCommentReplyId(String commentReplyId) {
        Example commentReplyExample = new Example(CommentReplys.class);
        Criteria criteria = commentReplyExample.createCriteria();
        criteria.andEqualTo("commentReplyId", commentReplyId);
        CommentReplys result = commentReplysMapper.selectOneByExample(commentReplyExample);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryCommentReplysInfoByShoesId(Integer page, Integer pageSize, String shoesId) {
        PageHelper.startPage(page, pageSize);
        List<CommentReplys> list = commentReplysMapper.queryCommentReplysByShoesId(shoesId);
        PageInfo<CommentReplys> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }
}
